Stream is a form of abstraction for a sequence of bytes. Microsoft .NET libraries provide this abstraction by a Stream Class. Various concrete stream implementations exist, such as for operating system files, data in memory, network sockets, etc. Since the OPC UA files are also such sequences of bytes with all their typical properties, it makes sense to reuse the stream abstraction and provide a stream implementation base on OPC UA files.
Methods related to OPC UA file streams are all located in the IEasyUAFileTransferExtension2 Class. There is also an internal class inside OPC Data Client that implements the stream on an OPC UA file, but your code will never create an instance of that class directly. Instead, there are extension methods to open or create a stream, and by calling them you will obtain a stream object which behaves like any other stream in the .NET world.
This article describes API level 2++ for OPC UA file transfer; for an overview of API levels, see OPC UA File Transfer Client.
You may also want to look at OPC UA File Transfers internals in order to understand a bit of what is happening "under the hood".
The extension methods in the IEasyUAFileTransferExtension2 Class (e.g. those that open or create streams, or those that read or write the full file contents) indicate errors by throwing IOException and its derivations (such as FileNotFoundException) only (this is, leaving aside usage errors such as invalid inputs arguments, which can be prevented, and cannot happen in a properly written code). You can therefore simply "catch" the IOException around these methods calls, and handle it appropriately.
The error model for properties and methods on the actual OPC UA data streams is given by whatever Microsoft prescribes in the Stream Class documentation. Typically it is only the IOException (and its derivations) again that can be thrown (leaving aside usage errors), but you should check the documentation for every operation called individually.
Before you can read from an OPC UA file stream or write to it, you need to obtain the stream object by opening an existing OPC UA file, or creating a new one as part of the process. Following methods (some with various overloads) are available for that:
All these methods return a stream object that can subsequently be used to access the file data. Eventually, the stream object should be disposed of using the IDisposable.Dispose Method.
The actual reading from OPC UA file streams and writing to OPC UA file streams is done by methods on the Stream Class in precisely the same way as with any other type of stream. Here is what you are going to use most:
The example below shows how to read different sections from an OPC UA file stream.
Some tasks do not require you to work with the file in parts. You may just want to read the whole contents of a file into memory and process it from there. Or, you already have prepared an "image" of the file contents in memory, and just want to create a file that contains the data you have created.
In these cases, you can take a shortcut, and use extension methods designed for that purpose. Internally, the methods are implemented using OPC UA file streams, and share the same internals, and error model, with them. They are:
The example below shows how to read the full contents of an OPC UA file at once, using the file transfer client.
The example below shows how to write the full contents of an OPC UA file at once, using the file transfer client.
For reading and writing primitive data types as binary values in a specific encoding, or reading and writing sequential series of characters (text), Microsoft .NET contains "reader" and "writer" classes which make these tasks easier. Examples of these classes are:
All these classes operate on top of existing streams. It can be any type of stream, and OPC UA file stream work with the "readers" and "writers" just fine.
You can create all above "readers" and "writers" using their constructors and passing them an already existing OPC UA file stream, but OPC Data Client also gives you some methods to create them directly:
The example below shows how to open an OPC UA file stream for reading, and read its content using a text reader object.